package com.glodon.paas.search.query;

import static org.elasticsearch.index.query.FilterBuilders.*;
import static org.elasticsearch.index.query.QueryBuilders.*;

import org.apache.commons.lang.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.glodon.paas.search.model.IndexDocument;
import com.glodon.paas.search.model.QueryDocument;

/**
 * 查询服务
 * 
 * @author botao.liao
 * 
 */
@Component("documentQueryService")
public class DocumentQueryService extends AbstractQueryService<IndexDocument, QueryDocument> {
    @Value("${search.document.fields}")
    private String fields;

    public QueryBuilder prepareQueryBuilder(QueryDocument file) {
        BoolQueryBuilder q = boolQuery();
        if (StringUtils.isNotEmpty(file.getId())) {
            q.must(termQuery("id", file.getId()));
        }
        if (StringUtils.isNotEmpty(file.getDatabagId())) {
            q.must(termQuery("databagId", file.getDatabagId()));
        }
        if (StringUtils.isNotEmpty(file.getName())) {
            q.must(queryString(file.getName()).field("name"));
        }
        if (StringUtils.isNotEmpty(file.getType())) {
            q.must(termQuery("type", file.getType()));
        }
        if (StringUtils.isNotEmpty(file.getFileType())) {
            q.must(termQuery("fileType", file.getFileType()));
        }
        if (StringUtils.isNotEmpty(file.getParentId())) {
            q.must(termQuery("parentId", file.getParentId()));
        }
        if (StringUtils.isNotEmpty(file.getExtension())) {
            q.must(termQuery("extension", file.getExtension()));
        }
        if (StringUtils.isNotEmpty(file.getContent())) {
            q.must(queryString(file.getContent()).field("content").field("name"));
        }
        if (StringUtils.isNotEmpty(file.getParentId())) {
            q.must(termQuery("projectId", file.getProjectId()));
        }
        if (file.getCreateTimeFrom() != null && file.getCreateTimeTo() != null) {
            q.must(rangeQuery("createTime").from(file.getCreateTimeFrom()).to(file.getCreateTimeTo()));
        }
        if (file.getUpdateTimeFrom() != null && file.getUpdateTimeTo() != null) {
            q.must(rangeQuery("updateTime").from(file.getUpdateTimeFrom()).to(file.getUpdateTimeTo()));
        }
        return q;
    }

    @Override
    public FilterBuilder prepareFilterBuilder(QueryDocument file) {
        if (StringUtils.isNotEmpty(file.getOwnerId())) {
            return orFilter().add(termFilter("ownerId", file.getOwnerId()))
                    .add(termFilter("shares", file.getOwnerId()));
        }
        return null;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }

    @Override
    public String getFields() {
        return fields;
    }

    @Override
    public IndexDocument createIndexObject() {
        return new IndexDocument();
    }
}
